package com.yonyou.cyxdms.sysAuth.service.service.dealerchannel;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.dto.dealer.MsgRecordNoticeDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealer.MsgRecordNoticePO;
import com.yonyou.cyxdms.sysAuth.service.mapper.MsgRecordNoticeMapper;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author adu
 * @since 2019-10-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MsgRecordNoticeServiceImpl extends ServiceImpl<MsgRecordNoticeMapper, MsgRecordNoticePO> implements MsgRecordNoticeService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(MsgRecordNoticeServiceImpl.class);
    @Autowired
    MsgRecordNoticeMapper msgRecordNoticeMapper;

    /**
     * 分页查询对应数据
     *
     * @param page           分页对象
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map < java.lang.String, java.lang.Object>>
     * @author adu
     * @since 2018/7/22 0022
     */
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(msgRecordNoticeMapper.selectPageBySql(page, queryCondition));
        return page;
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author adu
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public List<Map<String, Object>> selectListBySql(Map<String, Object> queryCondition) {
        return msgRecordNoticeMapper.selectListBySql(queryCondition);
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param id 主键ID
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author adu
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getById(Long id) {
        MsgRecordNoticePO msgrecordnoticepo = msgRecordNoticeMapper.selectById(id);
        if (msgrecordnoticepo != null) {
            return msgrecordnoticepo.toMaps();
        } else {
            throw new DALException("查询失败,没有符合条件的数据");
        }
    }

    /**
     * 根据ID 进行删除数据
     *
     * @param id
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteById(Long id) {
        int deleteCount = msgRecordNoticeMapper.deleteById(id);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount <= 0) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    /**
     * 根据ids 进行删除
     *
     * @param ids
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteBatchIds(String ids) {
        List<Long> longList = StringUtils.convertStrToArray(ids, ",", Long.class);
        int deleteCount = msgRecordNoticeMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}", deleteCount);
        if (deleteCount != longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    /**
     * 查询-公告维护（分页）
     * adu
     *
     * @param page
     * @param queryCondition
     * @return
     */
    @Override
    public IPage<Map<String, Object>> selectPageByNotice(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(msgRecordNoticeMapper.selectPageByNotice(page, queryCondition));
        return page;
    }

    /**
     * 新增-公告维护
     * adu
     *
     * @param page
     * @param queryCondition
     * @return
     */
    @Override
    public Integer addNotice(MsgRecordNoticeDTO msgRecordNoticeDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        msgRecordNoticeDTO.setAppId(loginInfoDto.getAppId());
        Map<String, Object> queryCondition = new HashMap<>();
        //对对象进行赋值操作
        MsgRecordNoticePO msgrecordnoticepo = msgRecordNoticeDTO.transDtoToPo(MsgRecordNoticePO.class);
        //执行插入
        return msgRecordNoticeMapper.addNotice(msgrecordnoticepo);
    }

    /**
     * 根据DTO 及ID 进行数据更新
     *
     * @param id                 主键ID
     * @param msgRecordNoticeDTO 页面DTO
     * @author adu
     * @since 2019-10-14
     */
    @Override
    public Map<String, Object> updateReleaseStatus(Integer id, MsgRecordNoticeDTO msgRecordNoticeDTO) {
        MsgRecordNoticePO msgrecordnoticepo = msgRecordNoticeMapper.selectById(id);
        msgrecordnoticepo.setPeleaseStatus(msgRecordNoticeDTO.getPeleaseStatus());
        //对对象进行赋值操作
        msgRecordNoticeDTO.transDtoToPo(msgrecordnoticepo);
        //执行更新
        msgRecordNoticeMapper.updateById(msgrecordnoticepo);
        return msgrecordnoticepo.toMaps();
    }
}
